package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Spending;
import cn.wolfcode.crm.domain.PotentialStudent;
import cn.wolfcode.crm.mapper.SpendingMapper;
import cn.wolfcode.crm.query.SpendingQueryObject;
import cn.wolfcode.crm.service.ISpendingService;
import cn.wolfcode.crm.service.IPotentialStudentService;
import cn.wolfcode.crm.service.ISpendingService;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class SpendingServiceImpl implements ISpendingService {
    @Autowired
    private SpendingMapper spendingMapper;


    @Override
    public PageResult getAll(SpendingQueryObject qo) {
        int count = spendingMapper.queryCount(qo);
        if (count == 0) {
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<Spending> data = spendingMapper.queryList(qo);
        return new PageResult(count, data);
    }

    @Override
    public void saveOrUpdate(Spending spending) {
        if (spending.getId() != null) {
           spendingMapper.updateByPrimaryKey(spending);
        }else {
            spending.setTime(new Date());
            spending.setState(false);
            spendingMapper.insert(spending);
        }
    }

    @Override
    public void audit(Long id) {
        Spending spending = spendingMapper.selectByPrimaryKey(id);
        if (!spending.isState()) {
            Employee employee = UserContext.getUserContext();
            spending.setAuditMan(employee);
            spending.setState(true);
            spendingMapper.audit(spending);
        }else {
            throw new RuntimeException("已经审核!!!!");
        }
    }

    @Override
    public void delete(Long id) {
        if (id != null) {
            spendingMapper.deleteByPrimaryKey(id);
        }else {
            throw new RuntimeException("数据异常");
        }
    }



    @Override
    public void theAudit(long id) {
        Spending spending = spendingMapper.selectByPrimaryKey(id);
        spending.setState(!spending.isState());
        spending.setAuditMan(UserContext.getUserContext());
        spendingMapper.audit(spending);
    }


    @Override
    public List<Spending> All() {
        return spendingMapper.selectAll();
    }
}
